<?php
class AdminList {
	private $db;
	private $tablePay;
	private $tableUserinfo;
	public function __construct() {
		$this->db = new Mysql ();
	
	}
	/**
	 * 
	 * 在线时长统计
	 */
	public function totalOnlinetime() {
		$offLineRule = "DATE_FORMAT(CURDATE(),'%Y%m%d')=LEFT(stoptime, 8) AND DATE_FORMAT(CURDATE(),'%Y%m%d')=LEFT(starttime, 8) AND stoptime IS NOT NULL";
		$todayOnlineCountRule = "DATE_FORMAT(CURDATE(),'%Y%m%d')=LEFT(starttime, 8) AND stoptime is NULL ";
		$befOnlineCountRule = "date_format(curdate(),'%Y%m%d')>left(starttime, 8) and stoptime is NULL";
		
		$sql = "SELECT phonetype, netstate,
		COUNT(DISTINCT phone,IF($offLineRule,1,NULL)) AS offlineCountPhone,
		COUNT(DISTINCT phone,IF($todayOnlineCountRule,1,NULL)) AS onlineCountPhone,
		COUNT(DISTINCT phone,IF($befOnlineCountRule,1,NULL)) AS befOnlineCountPhone,
		
		SUM( IF($offLineRule ,
					UNIX_TIMESTAMP( STR_TO_DATE( stoptime , '%Y%m%d%H%i%s' ) ) - UNIX_TIMESTAMP( STR_TO_DATE( starttime , '%Y%m%d%H%i%s' ) ) ,0
					)) AS offlineCount,
		
		SUM(IF( $todayOnlineCountRule,
				UNIX_TIMESTAMP(SYSDATE()) - UNIX_TIMESTAMP(STR_TO_DATE(starttime, '%Y%m%d%H%i%s')),0
					)) AS todayOnlineCount, 
		
		SUM(IF( $befOnlineCountRule ,
			UNIX_TIMESTAMP(SYSDATE()) - UNIX_TIMESTAMP(CURDATE()),0)) AS befDayOnlineCount
		
		FROM T_NetStateChange 
		
		GROUP BY phonetype,netstate
		";
		
		return $this->db->selectAll ( $sql );
	}
	/**
	 * 
	 * 活跃用户统计
	 */
	public function totalActUser() {
		$sql = "SELECT * FROM t_aliveuser ";
		//设置分页信息
		$page = new Page ();
		if (Http::get ( 'orderby' )) {
			$page->orderBy = Http::get ( 'orderby' );
			$page->desc = Http::get ( 'sc' );
		} else {
			$page->orderBy = 'curdate';
		}
		$page->uriStyle = '?';
		$list = $page->getPage ( $sql, 0 );
		$html = $page->htmlPage ();
		//返回搜索结果和分页信息
		return array ('list' => $list, 'html' => $html, 'total' => $page->allNums, 'eachNums' => $page->eachNums );
	}
	
	public function userLog() {
		$sql = "SELECT * FROM login_log WHERE 1 ";
		//搜索条件过滤
		if (Http::get ( 'stype' )=='uid' and Http::get ( 'uid' )) {
			$sql = $sql . " AND uid = '" . Http::get ( 'uid' ) . "' ";
		}
		if (Http::get ( 'stype' )=='phone' and Http::get ( 'uid' )) {
			$sql = $sql . " AND phone = '" . Http::get ( 'uid' ) . "' ";
		}
		if (Http::get ( 'os_type' )) {
			$sql = $sql . " AND os_type = '" . Http::get ( 'os_type' ) . "' ";
		}
		
		if (Http::get ( 'stimeline' )) {
			$sql = $sql . " AND time >= '" . Http::get ( 'stimeline' ) . "' ";
		}
		if (Http::get ( 'etimeline' )) {
			$sql = $sql . " AND time <= '" . Http::get ( 'etimeline' ) . "' ";
		}
		
		//设置分页信息
		$page = new Page ();
		if (Http::get ( 'orderby' )) {
			$page->orderBy = Http::get ( 'orderby' );
			$page->desc = Http::get ( 'sc' );
		} else {
			$page->orderBy = 'id';
		}
		$page->uriStyle = '?';
		$list = $page->getPage ( $sql, 0 );
		$html = $page->htmlPage ();
		//返回搜索结果和分页信息
		return array ('list' => $list, 'html' => $html, 'total' => $page->allNums, 'eachNums' => $page->eachNums );
	}
}